package ro.prjmanager.service;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;
import ro.prjmanager.core.service.AbstractService;
import ro.prjmanager.core.usr.UserSession;
import ro.prjmanager.dao.UsersDao;
import ro.prjmanager.model.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;


public class LoginService extends AbstractService implements LoginInterfaceService {
    private UsersDao usersDao;
    private static final Log log= LogFactoryImpl.getLog(LoginService.class) ;


    public UserSession getLogin(String userName, String password) {
        UserSession<Users,Projects,Svn,DataBases,Schema> ret = null;
        Users users = new Users();
        users.setPassword(password);
        users.setUserName(userName);
        List<Users> foundUsersList = getUsersDao().find(users);
        if (foundUsersList != null && !foundUsersList.isEmpty()) {
            ret=new UserSession<Users,Projects,Svn,DataBases,Schema>();
            users = foundUsersList.get(0);
            ret.setUser(users);
            List<UserSvnProject> userSvnProjects = users.getFkUserUserSvnProject();
            if (userSvnProjects != null && !userSvnProjects.isEmpty()) {
                Projects projects = userSvnProjects.get(0).getFkProject();
                Projects prjNew=new Projects();
                try {
                    BeanUtils.copyProperties(prjNew,projects);
                } catch (IllegalAccessException e) {
                    log.error(e);
                } catch (InvocationTargetException e) {
                    log.error(e);
                }
                ret.setProject(prjNew);
                List<ProjectDb> projectDbs = projects.getFkProjectProjectDb();
                if (projectDbs != null && !projectDbs.isEmpty()) {
                    DataBases dataBases=projectDbs.get(0).getFkDbSchema().getFkDatabase();
                    DataBases dataBasesNew=new DataBases();
                    try {
                        BeanUtils.copyProperties(dataBasesNew,dataBases);
                    } catch (IllegalAccessException e) {
                        log.error(e);
                    } catch (InvocationTargetException e) {
                        log.error(e);
                    }

                    ret.setDataBases(dataBasesNew);
                    Schema schema=projectDbs.get(0).getFkDbSchema().getFkSchema();
                    Schema schemaNew=new Schema();
                    try {
                        BeanUtils.copyProperties(schemaNew,schema);
                    } catch (IllegalAccessException e) {
                        log.error(e);
                    } catch (InvocationTargetException e) {
                        log.error(e);
                    }
                    ret.setSchema(schemaNew);
                }

                Svn svn = userSvnProjects.get(0).getFkSvn();
                Svn svnNew=new Svn();

                try {
                    BeanUtils.copyProperties(svnNew,svn);
                    svnNew.setUserName(users.getUserName());
                    svnNew.setPassword(users.getPassword());
                } catch (IllegalAccessException e) {
                    log.error(e);
                } catch (InvocationTargetException e) {
                    log.error(e);
                }


                ret.setSvn(svnNew);
            }
        }

        return ret;

    }

    public UserSession getLogin(String userName, String password, String dbName, String schema, String svnName) {
        return null;
    }

    public void create(Object entity) {

    }

    public void delete(Object o) {

    }

    public Object load(Object o) {
        return null;
    }

    public List listPag(int startPage, int resPerPage) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public List listPag(int startPage, int resPerPage, String orderCol, boolean asc, Object[] otherItems) {
        return usersDao.listPag(startPage, resPerPage,orderCol, asc, otherItems);
    }

    public List list() {
        return usersDao.findAll();
    }

    public List listPagFilter(int startPage, int resPerPage, String filter) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public int no(String filter) {
        return usersDao.no(filter);
    }

    public void update(Object entity) {
        
    }

    public UsersDao getUsersDao() {
        return usersDao;
    }

    public void setUsersDao(UsersDao usersDao) {
        this.usersDao = usersDao;
    }
}
